/**
 * Created by hsia on 2017/4/12.
 */
var cycleChargeJs = function(targetObj){
    this.sysUtils = new kendoUtils();
    this.targetObj = targetObj;
    this.init();
    return this;
};
cycleChargeJs.prototype={
    init:function(){
        var bindFieldArray = this.sysUtils.getBindFieldArray(this.targetObj);
        var bindFunctionArray = [
            {fName:"savechargeInfo",fs:this.savechargeInfo},
            {fName:"cancel",fs:function(){$("#"+chargeList.monthChargeDivId).data("kendoWindow").close();}}];
        this.bindBaseModels = new bindBaseModel(null,bindFieldArray,bindFunctionArray,null);
        this.sysUtils.bindDoucment(this.bindBaseModels,this.targetObj,this);
        var payType = Utils.getModelValueByModelKey(BASIS.CHARGE_MONTH_PAY.MONTH);
        Utils.createDataDictDropDownList([{div:this.targetObj.find("#payType"),parentModelKey:BASIS.CHARGE_MONTH_PAY.ROOT,select:payType}],this);
        this.loadChargeInfo()
    },
    //保存周期收费信息
    savechargeInfo:function(e){
        var that =  $(e.target).data("that");
        if(validityForm(that.targetObj)){
            var chargeInfo = that.sysUtils.getBindDocJson();
            var url = baseApiPath+"chargeStandard/save?isEdit=" + chargeList.isEdit;
            var sendJson=chargeInfo;
            for(var o in chargeInfo){
                if(o.indexOf('_') !=-1){
                    var strArray = o.substring(o.indexOf('_')+1);
                    var name = o.substring(0, o.indexOf('_'));
                    var jsonA = {};
                    jsonA[name] = that.recursionObjectName(strArray,chargeInfo[o]) ;
                    delete chargeInfo[o];
                    var sendJson =$.extend(true,chargeInfo,jsonA);
                }
            }
            if(!chargeList.isEdit){
                sendJson["chargeType"]=0;
                sendJson["chargeParamCode"]='cycle';
            }
            JsonpAjax(url,JSON.stringify(sendJson),"json",function(result){
                $("#"+chargeList.monthChargeDivId).data("kendoWindow").close();
                chargeList.findGridData();
            });
        }
    },
    recursionObjectName : function (strArray,val) {
        if (strArray.indexOf('_') == -1){
            var jsonC = {};
            jsonC[strArray]=val;
            return jsonC;
        }
        var name = strArray.substring(0, strArray.indexOf('_'));
        strArray = strArray.substring(strArray.indexOf('_')+1);
        var jsonB = {};
        jsonB[name] = this.recursionObjectName(strArray,val);
        return jsonB;
    },
    loadChargeInfo : function(e) {
        var that = this;
        if (!isEmpty(e)) {
            that = $(e.target).data("that");
        }
        if (null == chargeList.editResult) return; //新增啥都不需要做
        this.sysUtils.setVariantBindFieldValue(chargeList.editResult);
        that.chargeStandardInfos = chargeList.editResult.chargeStandardInfos;
    }
}